package com.cy.personer.web.userWeb;

import com.cy.personer.bean.*;
import com.cy.personer.biz.AdvertBiz;
import com.cy.personer.biz.impl.CardBizImpl;
import com.cy.personer.biz.impl.UserBizImpl;
import com.cy.personer.biz.impl.UserMenuBizImpl;
import com.cy.personer.bean.User;
import com.cy.personer.biz.UserBiz;
import com.cy.personer.mapper.ReportSxbMapper;
import com.cy.personer.web.MD5Encoder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/frontUser")
public class FrontUserHandler {
    @Resource
    private CardBizImpl cardBizImpl;
    @Resource
    private UserMenuBizImpl userMenuBizImpl;
    @Resource
    private UserBizImpl userBizImpl;
    @Resource
    private MD5Encoder md5Encoder;
    @Resource
    private AdvertBiz advertBizImpl;

    //打开网站
    @RequestMapping(value ="/showIndex.action")
    public void showIndex(HttpServletRequest request,HttpServletResponse resp) throws IOException {
        HttpSession session = request.getSession();
        List<UserMenu> UserMenu = userMenuBizImpl.queryUserMenu();
        session.setAttribute("UserMenu",UserMenu);
        resp.sendRedirect(request.getContextPath()+"/index.jsp");
    }


    //网站首页显示
    @RequestMapping(value ="/showUserIndex.action")
    public String showUserIndex(HttpServletRequest request,HttpServletResponse resp) throws IOException {
        List<Advert>list;
        list=advertBizImpl.quers();
        if (list!=null){
            request.setAttribute("list",list);
            return "user/userIndex";
        }else
            return "user/userIndex";

    }


    //展示用户登录
    @RequestMapping(value ="/showUserLogin.action")
    public String showUserLogin(){
        return "user/userLogin";
    }

    //检测用户
    @RequestMapping(value="/checkLogin.action", method=RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody String checkLogin(HttpServletRequest request,User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String result = "loginError";
        HttpSession session = request.getSession();
//        ServletContext application = session.getServletContext();
        String keycode = user.getKeyCode().toUpperCase();
        String sessionCode = (String) request.getSession().getAttribute("keyCode");
//        User frontUser1 = (User) application.getAttribute("frontUser");
//        if (frontUser1 == null){
        if(keycode.equals(sessionCode)){
            String password = md5Encoder.EncoderByMd5(user.getUserPwd());
            user.setUserPwd(password);
            User frontUser = userBizImpl.userLogin(user);
            if(frontUser != null){
                if(frontUser.getStateId()==0||frontUser.getStateId()==2){
                    result = "stateError";//帐号状态不可用
                }else{
                    session.setAttribute("frontUser",frontUser);
//                    application.setAttribute("frontUser",frontUser);
                    result = "success";
                }
            }else{
                result = "loginError";//帐号或密码错误
            }
        }else{
            result = "codeError";//验证码错误
        }
//        }else {
//            result = "existence";//帐号已经登录
//        }
        return result;
    }

    //登录
    @RequestMapping(value="/userLogin.action")
    public void loginUser(HttpServletRequest request,HttpServletResponse resp) throws IOException {
        resp.sendRedirect(request.getContextPath()+"/index.jsp");
    }

    //展现用户注册页面
    @RequestMapping(value ="/showRegisterUser.action")
    public String showRegisterUser(){
        return "user/userRegister";
    }

    //检测该卡号是否可用
    @RequestMapping(value ="/cardNumberCheck.action")
    public @ResponseBody String cardNumberCheck (HttpServletRequest request,String cardNumber,Card card){
        String result = "error";
        User user2 = userBizImpl.cardNumberCheck(cardNumber);//检测该卡是否已被使用了
        Card card1 = cardBizImpl.checkCard(cardNumber);//检测该卡是否存在卡表里
        if (user2 != null){
            result = "success";
        }else if (card1 != null){
            result = "error";
        }else {
            card.setStateId(1);
            cardBizImpl.addCard(card);
        }

        return result;
    }

    //验证用户名是否可注册
    @RequestMapping(value="/userAccountCheck.action")
    public @ResponseBody String userAccountCheck(String userAccount){
        String result = "error";
        User user = userBizImpl.userAccountCheck(userAccount);
        if(user!=null){
            result = "success";
        }
        return result;
    }

    //注册页面
    @RequestMapping(value ="/registerUser.action")
    public @ResponseBody String registerUser (HttpServletRequest request,User user,String cardNumber) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String result1 = "error";
        Card card = cardBizImpl.checkCard(cardNumber);
        if (card != null){
            user.setCardId(card.getCardId());
        }
        String password = md5Encoder.EncoderByMd5(user.getUserPwd());
        user.setUserPwd(password);
        user.setStateId(1);
        boolean result = userBizImpl.userRegister(user);
        if (result){
            result1 = "success";
        }
        return result1;
    }


    //退出
    @RequestMapping(value="/userOut.action")
    public void out(HttpServletRequest request,HttpServletResponse resp) throws IOException {
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        session.removeAttribute("frontUser");
        application.removeAttribute("frontUser");
        resp.sendRedirect(request.getContextPath()+"/index.jsp");
    }


    @RequestMapping("/getUserData.action")
    public String getUserData(int userId){
        return "";
    }

}
